package com.globo.opensocial;

import java.io.File;

import org.htmlparser.nodes.TagNode;

import com.globo.opensocial.tags.ForTag;
import com.globo.opensocial.tags.IfTag;
import com.globo.opensocial.tags.IncludeTag;

public class Util {

	static public void creteElementById(String name, final String instance,
			StringBuilder out) {
		out.append("var ");
		out.append(instance);
		out.append(" = osg.E(\"");
		out.append(name);
		out.append("\");\n");
	}
	
	

	/**
	 * Returns the method name.
	 * 
	 * @param fileName
	 * @param prefix
	 * @return
	 */
	static public String getMethodName(String fileName, String prefix) {
		return getMethodName(fileName, prefix, "Page");
	}	

	static public String getKey( String fileName ) {
		
		String name = new File(fileName).getName();;
		return name.split("\\.")[0].toLowerCase();
		
	}
	
	
	/**
	 * Gera o nome do método no Javascript baseado no nome do arquivo.
	 * 
	 * @param fileName
	 *            nome do arquivo
	 * @return nome do médoto
	 */
	static public String getMethodName(String fileName, String prefix,
			String suffix) {

		// new File()
		File f = new File(fileName);

		// logger.info(f.getName());
		fileName = f.getName();

		// ignore template files
		if (fileName.startsWith(".") || fileName.startsWith("_")) {
			return null;
		}

		int indexOfDot = fileName.indexOf('.');
		if (indexOfDot > -1) {
			fileName = fileName.substring(0, indexOfDot);
		}

		String methodName = "";
		for (String token : fileName.split("_")) {

			methodName += token.substring(0, 1).toUpperCase();
			methodName += token.substring(1); // .toLowerCase();
		}
		return prefix + methodName + suffix;

	}


	
	
	
	/**
	 * Check if node is a special-tag.
	 * @param node
	 * @return
	 */
	public static boolean isSpecialTag(TagNode node) {
		
		if ( node instanceof IfTag ) {
			return true;
		}
		if ( node instanceof ForTag ) {
			return true;
		}
		if ( node instanceof IncludeTag ) {
			return true;
		}

		return false;
	}



	public static void appendInclude(String template, StringBuilder out) {
		String key = getKey(template);
		if ( key != null ) {
			out.append("(new osg.Template('"+key+"').append(this).show())");
		}
	}
	

}
